####################################################
# GETCOAUTHNETWK.R
#
# This function construct a network from the
# author data set.
#
#This function computes 
#
# Arguments:
#   startyear               
#   endyear    
#   attr please add the file with the vertices attribute            
#
# Author: Lorenzo Ductor
######################################################

library(igraph)
library(foreign)
getnetwork<-function(startyear,endyear, data){
# 7 authors

auth712<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth1','auth2')]
auth713<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth1','auth3')]
auth714<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth1','auth4')]
auth715<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth1','auth5')]
auth716<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth1','auth6')]
auth717<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth1','auth7')]
auth723<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth2','auth3')]
auth724<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth2','auth4')]
auth725<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth2','auth5')]
auth726<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth2','auth6')]
auth727<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth2','auth7')]
auth734<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth3','auth4')]
auth735<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth3','auth5')]
auth736<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth3','auth6')]
auth737<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth3','auth7')]
auth745<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth4','auth5')]
auth746<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth4','auth6')]
auth747<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth4','auth7')]
auth756<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth5','auth6')]
auth757<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth5','auth7')]
auth767<-data[data$nauthors==7 & data$year>=startyear & data$year<endyear+1, c('auth6','auth7')]

colnames(auth712)[2] <- "auth2"
colnames(auth713)[2] <- "auth2"
colnames(auth714)[2] <- "auth2"
colnames(auth715)[2] <- "auth2"
colnames(auth716)[2] <- "auth2"
colnames(auth717)[2] <- "auth2"
colnames(auth723)[2] <- "auth2"
colnames(auth723)[1] <- "auth1"
colnames(auth724)[2] <- "auth2"
colnames(auth724)[1] <- "auth1"
colnames(auth725)[2] <- "auth2"
colnames(auth725)[1] <- "auth1"
colnames(auth726)[2] <- "auth2"
colnames(auth726)[1] <- "auth1"
colnames(auth727)[2] <- "auth2"
colnames(auth727)[1] <- "auth1"
colnames(auth734)[1] <- "auth1"
colnames(auth734)[2] <- "auth2"
colnames(auth735)[1] <- "auth1"
colnames(auth735)[2] <- "auth2"
colnames(auth736)[1] <- "auth1"
colnames(auth736)[2] <- "auth2"
colnames(auth737)[1] <- "auth1"
colnames(auth737)[2] <- "auth2"
colnames(auth745)[1] <- "auth1"
colnames(auth745)[2] <- "auth2"
colnames(auth746)[1] <- "auth1"
colnames(auth746)[2] <- "auth2"
colnames(auth747)[1] <- "auth1"
colnames(auth747)[2] <- "auth2"
colnames(auth756)[1] <- "auth1"
colnames(auth756)[2] <- "auth2"
colnames(auth757)[1] <- "auth1"
colnames(auth757)[2] <- "auth2"
colnames(auth767)[1] <- "auth1"
colnames(auth767)[2] <- "auth2"

auth7<-rbind(auth712,auth713, auth714, auth715, auth716, auth717, auth723, auth724, auth725,auth726, auth727, auth734, auth735,auth736, auth737, auth745, auth746, auth747, auth756, auth757, auth767)


# 6 authors
auth612<-data[data$nauthors==6 & data$year>=startyear & data$year<endyear+1, c('auth1','auth2')]
auth613<-data[data$nauthors==6 & data$year>=startyear & data$year<endyear+1, c('auth1','auth3')]
auth614<-data[data$nauthors==6 & data$year>=startyear & data$year<endyear+1, c('auth1','auth4')]
auth615<-data[data$nauthors==6 & data$year>=startyear & data$year<endyear+1, c('auth1','auth5')]
auth616<-data[data$nauthors==6 & data$year>=startyear & data$year<endyear+1, c('auth1','auth6')]
auth623<-data[data$nauthors==6 & data$year>=startyear & data$year<endyear+1, c('auth2','auth3')]
auth624<-data[data$nauthors==6 & data$year>=startyear & data$year<endyear+1, c('auth2','auth4')]
auth625<-data[data$nauthors==6 & data$year>=startyear & data$year<endyear+1, c('auth2','auth5')]
auth626<-data[data$nauthors==6 & data$year>=startyear & data$year<endyear+1, c('auth2','auth6')]
auth634<-data[data$nauthors==6 & data$year>=startyear & data$year<endyear+1, c('auth3','auth4')]
auth635<-data[data$nauthors==6 & data$year>=startyear & data$year<endyear+1, c('auth3','auth5')]
auth636<-data[data$nauthors==6 & data$year>=startyear & data$year<endyear+1, c('auth3','auth6')]
auth645<-data[data$nauthors==6 & data$year>=startyear & data$year<endyear+1, c('auth4','auth5')]
auth646<-data[data$nauthors==6 & data$year>=startyear & data$year<endyear+1, c('auth4','auth6')]
auth656<-data[data$nauthors==6 & data$year>=startyear & data$year<endyear+1, c('auth5','auth6')]
colnames(auth612)[2] <- "auth2"
colnames(auth613)[2] <- "auth2"
colnames(auth614)[2] <- "auth2"
colnames(auth615)[2] <- "auth2"
colnames(auth616)[2] <- "auth2"
colnames(auth623)[2] <- "auth2"
colnames(auth623)[1] <- "auth1"
colnames(auth624)[2] <- "auth2"
colnames(auth624)[1] <- "auth1"
colnames(auth625)[2] <- "auth2"
colnames(auth625)[1] <- "auth1"
colnames(auth626)[2] <- "auth2"
colnames(auth626)[1] <- "auth1"
colnames(auth634)[1] <- "auth1"
colnames(auth634)[2] <- "auth2"
colnames(auth635)[1] <- "auth1"
colnames(auth635)[2] <- "auth2"
colnames(auth636)[1] <- "auth1"
colnames(auth636)[2] <- "auth2"
colnames(auth645)[1] <- "auth1"
colnames(auth645)[2] <- "auth2"
colnames(auth646)[1] <- "auth1"
colnames(auth646)[2] <- "auth2"
colnames(auth656)[1] <- "auth1"
colnames(auth656)[2] <- "auth2"
auth6<-rbind(auth612,auth613, auth614, auth615, auth616, auth623, auth624, auth625,auth626, auth634, auth635,auth636, auth645, auth646, auth656)


# 5 authors
auth512<-data[data$nauthors==5 & data$year>=startyear & data$year<endyear+1, c('auth1','auth2')]
auth513<-data[data$nauthors==5 & data$year>=startyear & data$year<endyear+1, c('auth1','auth3')]
auth514<-data[data$nauthors==5 & data$year>=startyear & data$year<endyear+1, c('auth1','auth4')]
auth515<-data[data$nauthors==5 & data$year>=startyear & data$year<endyear+1, c('auth1','auth5')]
auth523<-data[data$nauthors==5 & data$year>=startyear & data$year<endyear+1, c('auth2','auth3')]
auth524<-data[data$nauthors==5 & data$year>=startyear & data$year<endyear+1, c('auth2','auth4')]
auth525<-data[data$nauthors==5 & data$year>=startyear & data$year<endyear+1, c('auth2','auth5')]
auth534<-data[data$nauthors==5 & data$year>=startyear & data$year<endyear+1, c('auth3','auth4')]
auth535<-data[data$nauthors==5 & data$year>=startyear & data$year<endyear+1, c('auth3','auth5')]
auth545<-data[data$nauthors==5 & data$year>=startyear & data$year<endyear+1, c('auth4','auth5')]

colnames(auth512)[2] <- "auth2"
colnames(auth513)[2] <- "auth2"
colnames(auth514)[2] <- "auth2"
colnames(auth515)[2] <- "auth2"
colnames(auth523)[2] <- "auth2"
colnames(auth523)[1] <- "auth1"
colnames(auth524)[2] <- "auth2"
colnames(auth524)[1] <- "auth1"
colnames(auth525)[2] <- "auth2"
colnames(auth525)[1] <- "auth1"
colnames(auth534)[1] <- "auth1"
colnames(auth534)[2] <- "auth2"
colnames(auth535)[1] <- "auth1"
colnames(auth535)[2] <- "auth2"
colnames(auth545)[1] <- "auth1"
colnames(auth545)[2] <- "auth2"
auth5<-rbind(auth512,auth513, auth514, auth515, auth523, auth524, auth525, auth534, auth535, auth545)


# 4 authors

auth412<-data[data$nauthors==4 & data$year>=startyear & data$year<endyear+1, c('auth1','auth2')]
auth413<-data[data$nauthors==4 & data$year>=startyear & data$year<endyear+1, c('auth1','auth3')]
auth414<-data[data$nauthors==4 & data$year>=startyear & data$year<endyear+1, c('auth1','auth4')]
auth423<-data[data$nauthors==4 & data$year>=startyear & data$year<endyear+1, c('auth2','auth3')]
auth424<-data[data$nauthors==4 & data$year>=startyear & data$year<endyear+1, c('auth2','auth4')]
auth434<-data[data$nauthors==4 & data$year>=startyear & data$year<endyear+1, c('auth3','auth4')]


colnames(auth412)[2] <- "auth2"
colnames(auth413)[2] <- "auth2"
colnames(auth414)[2] <- "auth2"
colnames(auth423)[2] <- "auth2"
colnames(auth423)[1] <- "auth1"
colnames(auth424)[2] <- "auth2"
colnames(auth424)[1] <- "auth1"
colnames(auth434)[1] <- "auth1"
colnames(auth434)[2] <- "auth2"

auth4<-rbind(auth412,auth413, auth414, auth423, auth424, auth434)

#Creating the network: appending the data to get two columns of authors

auth312<-data[data$nauthors==3 & data$year>=startyear & data$year<endyear+1,c('auth1','auth2')]
auth313<-data[data$nauthors==3 & data$year>=startyear & data$year<endyear+1, c('auth1','auth3')]
auth323<-data[data$nauthors==3 & data$year>=startyear & data$year<endyear+1, c('auth2','auth3')]

colnames(auth313)[2] <- "auth2"
colnames(auth323)[2] <- "auth2"
colnames(auth323)[1] <- "auth1"
auth3<-rbind(auth312,auth313,auth323)

auth2<-data[data$nauthors==2 & data$year>=startyear & data$year<endyear+1,c('auth1','auth2')]


alllinks<-rbind(auth2,auth3,auth4, auth5, auth6, auth7)

n1<-max(alllinks$auth1+1,alllinks$auth2+1)
g<-graph.empty(n1,directed=FALSE)
g<-add.edges(g,rbind(alllinks$auth1,alllinks$auth2))
#g2 <- graph.data.frame(alllinks, directed=FALSE)
g1<-simplify(g)
g1
}